/**
 * Created by kux on 2017/7/8.
 */
//根据canvas ID和图片加载图片
function drawBeauty(name,imgname,x,y){
    var mycv = document.getElementById(name);
    var pic=document.getElementById(imgname);
    var myctx = mycv.getContext("2d");
    myctx.drawImage(pic, 0,0,x*100,y*100);
}

//根据CanvasID和接受到的data-class为对应的canvas➕滤镜
function adjustColor(id,c) {    //接受一个ID值
    var canvas = document.getElementById(id);
    var ctx=canvas.getContext('2d')
    var len = canvas.width * canvas.height * 4;
    var canvasData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    var binaryData = canvasData.data;
    // tempContext.putImageData(startData, 0, 0);
    // Processing all the pixels
    switch (c){
        case '0':

            gfilter.colorInvertProcess(binaryData, len);
            console.log('滤镜0')
            break;

        case '1':

            gfilter.colorAdjustProcess(binaryData, len);
            console.log('滤镜1')
            break;

        case '2':
            console.log()
            // gfilter.gray(binaryData, len);
            color.gray(canvasData)
            break;
        case '3':
            console.log('浮雕效果')
            color.cameo(canvasData)
            break;
        case '4':
            console.log('连环画')
            color.comic(canvasData)
            break;
        default:
            console.log('dada')

    }

    ctx.putImageData(canvasData, 0, 0);
}

//根据ID生成图片
function takeScreenshot(id) {

    html2canvas(document.getElementById(id), {
        onrendered: function(canvas) {
            var a= document.getElementById('can').appendChild(canvas);
            // a.setAttribute('id','canpic')
            // convertCanvasToImage(a)
            window.sessionStorage.imgurl=a.toDataURL("image/png");
            // $('#'+id).hide();
            window.location.href='end.html'

        },

    });
}


function convertCanvasToImage(canvas) {
    var image = new Image();
    image.src = canvas.toDataURL("image/png");
    console.log( canvas.toDataURL("image/png"))
    return image;
}

var $U={
    get:function (url,data,_callback) {
        $.ajax({
            type:"GET",
            url:url,
            data :data,
            dataType:'json',
            // jsonp:'jsonCallBack',
            success:function(data){
              _callback(data)
            }
        });
    },
    post:function (url,data,_callback) {
        $.ajax({
            type:"POST",
            url:url,
            data :data,
            dataType:'json',
            // jsonp:'jsonCallBack',
            success:function(data){
                _callback(data)
            }
        });
    }
}


//上传图片
function setImagePreview() {
    var preview, img_txt, localImag, file_head = document.getElementById("file_head"),
        picture = file_head.value;
    if (!picture.match(/.jpg|.gif|.png|.bmp/i)) return alert("您上传的图片格式不正确，请重新选择！"),
        !1;
    if (preview = document.getElementById("preview"), file_head.files && file_head.files[0])
        preview.style.display = "block",
        preview.style.width = "63px",
        preview.style.height = "63px",
        preview.src = window.navigator.userAgent.indexOf("Chrome") >= 1 || window.navigator.userAgent.indexOf("Safari") >= 1 ? window.webkitURL.createObjectURL(file_head.files[0]) : window.URL.createObjectURL(file_head.files[0]);
    else {
        file_head.select(),
            file_head.blur(),
            img_txt = document.selection.createRange().text,
            localImag = document.getElementById("localImag"),
            localImag.style.width = "63px",
            localImag.style.height = "63px";
        try {
            localImag.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)",
                localImag.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = img_txt
        } catch(f) {
            return alert("您上传的图片格式不正确，请重新选择！"),
                !1
        }
        preview.style.display = "none",
            document.selection.empty()
    }
    return document.getElementById("DivUp").style.display = "block",
        !0
}



/*function uploadimg() {
    var piliang = document.querySelector('#files');
    piliang.addEventListener('change',function () {
        for (var i = 0;i < this.files.length;i++){
                var file = this.files[i];
                $('#img_box img').eq(i).attr('src',file);

            }
    });
}*/


$(function(){
    var a=[];
    $("#file_head").on("change",function () {
        a.push(0);
        var l=a.length-1;
        var $file = $(this);
        var fileObj = $file[0];
        var windowURL = window.URL || window.webkitURL;
        var dataURL;
        var $img = $("#img_box img").eq(l);

        if (fileObj && fileObj.files && fileObj.files[0]) {
            dataURL = windowURL.createObjectURL(fileObj.files[0]);
            $img.attr('src', dataURL);
        } else {
            dataURL = $file.val();
            var imgObj = document.getElementById("preview");
            imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
            imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;

        };

        setTimeout(function () {
            var c=document.getElementById("cv"+l);

            var pic=document.getElementById('pic'+l);
            var ctx=c.getContext("2d");
            ctx.drawImage(pic, 0,0,3.4*100,1.56*100);
        },500)


    });
});



$(function(){
    var a=[];
    $("#file_head").on("change",function () {
        a.push(0);
        var l=a.length-1;
        var $file = $(this);
        var fileObj = $file[0];
        var windowURL = window.URL || window.webkitURL;
        var dataURL;
        var $img = $("#img_box img").eq(l);

        if (fileObj && fileObj.files && fileObj.files[0]) {
            dataURL = windowURL.createObjectURL(fileObj.files[0]);
            $img.attr('src', dataURL);
        } else {
            dataURL = $file.val();
            var imgObj = document.getElementById("preview");
            imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
            imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;

        };

        setTimeout(function () {
            var c=document.getElementById("cv"+l);

            var pic=document.getElementById('pic'+l);
            var ctx=c.getContext("2d");
            ctx.drawImage(pic, 0,0,3.4*100,1.56*100);
        },500)

    });
});






